package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingBaseTaxTypeEntity;
import com.ly.heart.domain.dto.KingBaseTaxTypeExcelDto;
import com.ly.heart.mapper.KingBaseTaxTypeMapper;
import com.ly.heart.service.IKingBaseTaxTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.heart.util.ChineseToPinyin;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 基础数据-税收分类编码-物料中使用 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2024-08-06
 */
@Service
public class KingBaseTaxTypeServiceImpl extends ServiceImpl<KingBaseTaxTypeMapper, KingBaseTaxTypeEntity> implements IKingBaseTaxTypeService {

    @Override
    public AjaxResult queryList ( KingBaseTaxTypeEntity kingBaseTaxTypeEntity ) {
        LambdaQueryWrapper<KingBaseTaxTypeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        Page page = new Page ( kingBaseTaxTypeEntity.getPage (), kingBaseTaxTypeEntity.getLimit () );
        Page resultPage = this.page ( page, lambdaQueryWrapper );
        return AjaxResult.success ( resultPage );
    }


    @Override
    public AjaxResult saveSingle ( KingBaseTaxTypeEntity kingBaseTaxTypeEntity ) {
        kingBaseTaxTypeEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        String py = ChineseToPinyin.getFirstLetter ( "税收分类" );
        String format = String.format ( "%8d", count );
        kingBaseTaxTypeEntity.setDocNo ( py.concat ( format ) );
        kingBaseTaxTypeEntity.setCreateDate ( DateUtils.getNowDate () );
        kingBaseTaxTypeEntity.setCreateUserId ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        boolean save = this.save ( kingBaseTaxTypeEntity );
        if ( ! save ) {
            throw new CustomException ( "新增税收分类编码" );
        }
        return AjaxResult.success ();
    }


    /**
     * 解析上传Excel的数据并入库
     *
     * @param dataList
     * @return
     */
    @Override
    public AjaxResult saveBatchList ( List<KingBaseTaxTypeExcelDto> dataList ) {
        List<KingBaseTaxTypeEntity> kingBaseTaxTypeEntities = new ArrayList<> ();
        int count = this.count ();//0
        String py = ChineseToPinyin.getFirstLetter ( "税收分类" );
        for ( int i = 0; i < dataList.size (); i++ ) {
            KingBaseTaxTypeExcelDto dto = dataList.get ( i );
            KingBaseTaxTypeEntity kingBaseTaxTypeEntity = new KingBaseTaxTypeEntity ();
            kingBaseTaxTypeEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
            //每次count加1
            String format = String.format ( "%08d", count++ );
            kingBaseTaxTypeEntity.setDocNo ( py.concat ( format ) );
            kingBaseTaxTypeEntity.setTaxTypeCode ( dto.getTaxTypeCode () );
            kingBaseTaxTypeEntity.setParentCode ( dto.getParentCode () );
            kingBaseTaxTypeEntity.setTitle ( dto.getTitle () );
            kingBaseTaxTypeEntity.setSimpleTitle ( dto.getSimpleTitle () );
            kingBaseTaxTypeEntity.setTaxDesc ( dto.getTaxDesc () );
            kingBaseTaxTypeEntity.setTaxRate ( dto.getTaxRate () );
            kingBaseTaxTypeEntity.setPreferentialTaxPolicy ( dto.getPreferentialTaxPolicy () );
            kingBaseTaxTypeEntity.setTaxVersion ( dto.getTaxVersion () );
            kingBaseTaxTypeEntity.setIsPreset ( "是".equals ( dto.getIsPreset () ) ? 1 : 2 );
            kingBaseTaxTypeEntity.setSummaryItem ( "是".equals ( dto.getSummaryItem () ) ? 1 : 2 );
            kingBaseTaxTypeEntity.setCreateDate ( DateUtils.getNowDate () );
            kingBaseTaxTypeEntity.setCreateUserId ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
            kingBaseTaxTypeEntities.add ( kingBaseTaxTypeEntity );
        }
        boolean b = this.saveBatch ( kingBaseTaxTypeEntities );
        if ( ! b ) {
            throw new CustomException ( "导入文件失败！" );
        }
        return AjaxResult.success ();
    }

    @Override
    public AjaxResult deleteByIds ( String[] ids ) {
        List<KingBaseTaxTypeEntity> list = this.list ( new LambdaQueryWrapper<KingBaseTaxTypeEntity> ().in ( KingBaseTaxTypeEntity :: getId, ids ) );
        List<String> idList = list.stream ().filter ( tax -> tax.getIsDel () == 1 ).map ( KingBaseTaxTypeEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( tax -> {
            if ( tax.getIsDel () == 2 ) {
                stringBuilder.append ( tax.getTitle () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseTaxTypeEntity> ().
                set ( KingBaseTaxTypeEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingBaseTaxTypeEntity :: getUpdateDate, DateUtils.getNowDate () ).
                set ( KingBaseTaxTypeEntity :: getUpdateUserId, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                in ( KingBaseTaxTypeEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除税收分类编码失败！" );
        }
        String str = "删除税收分类编码成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    @Override
    public AjaxResult disabled ( String id ) {
        KingBaseTaxTypeEntity byId = this.getById ( id );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseTaxTypeEntity> ().
                set ( KingBaseTaxTypeEntity :: getIsDisabled, byId.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingBaseTaxTypeEntity :: getUpdateDate, DateUtils.getNowDate () ).
                set ( KingBaseTaxTypeEntity :: getUpdateUserId, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                eq ( KingBaseTaxTypeEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( byId.getIsDisabled () == 1 ? "禁用税收分类编码失败！" : "解禁税收分类编码失败！" );
        }
        return AjaxResult.success ();
    }

}
